/**
 *
 */
package com.bawei.cms.web.controllers;

import com.bawei.cms.domain.Article;
import com.bawei.cms.domain.Category;
import com.bawei.cms.domain.User;
import com.bawei.cms.service.ArticleService;
import com.bawei.cms.service.ChannelCategoryService;
import com.bawei.cms.web.Constant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 说明:
 *
 * @author howsun ->[howsun.zhang@gmail.com]
 * @version 1.0
 *
 * 2018年1月10日 下午2:40:38
 */
@Controller
@RequestMapping("/my")
public class UserController {

    @Resource
    private ArticleService articleService;

    @Resource
    private ChannelCategoryService channelCategoryService;

    @RequestMapping({"/", "/index", "/home"})
    public String home() {
        return "user-space/home";
    }

    @RequestMapping({"/profile"})
    public String profile() {
        return "user-space/profile";
    }

    /**
     * 进入发布文章
     * @param model
     * @return
     */
    @RequestMapping("/blog/edit")
    public String edit(Model model,String id) {
        if(StringUtil.isNotEmpty(id)){
            Article article = articleService.getById(id);
            model.addAttribute("article",article);
        }
        return "user-space/blog_edit";
    }

    /**
     * 发布文章
     * @param model
     * @return
     */
    @RequestMapping("/blog/save")
    public String save(Model model, Article article, MultipartFile file, HttpSession session) {
        try {
            if (file != null && file.getSize() > 0) {
                String filename = file.getOriginalFilename();
                String substring = filename.substring(filename.lastIndexOf("."));
                String savename = UUID.randomUUID().toString() + substring;
                file.transferTo(new File("D:\\tp\\" + savename));
                article.setPicture("/tp/" + savename);
            }
            Integer id = article.getId();
            //获取当前登录用户
            User loginUser = Constant.getLoginUser(session);
            article.setAuthor(loginUser);
            Date now= new Date();
            article.setUpdated(now);
            if(id !=null){
                articleService.update(article);
            } else{
                article.setCreated(now);
                article.setHits(0);//默认点击量是0
                article.setHot(false);//默认不上头条
                article.setDeleted(false);
                articleService.insert(article);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/my/blogs";
    }

    /**
     * 文章列表
     * @param model
     * @return
     */
    @RequestMapping("/blogs")
    public String blogs(Model model,Article article,
	   @RequestParam(defaultValue = "1")int pageNum,
		@RequestParam(defaultValue = "3")int pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		List<Article> list = articleService.list(article);
		PageInfo<Article> pageInfo = new PageInfo<>(list);
		model.addAttribute("pageInfo",pageInfo);
		model.addAttribute("article",article);
		return "user-space/blog_list";
    }
    /**
     * 进入发布文章
     */
    @RequestMapping("getById")
    @ResponseBody
    public Article getById(String id) {
        return articleService.getById(id);
    }

    @RequestMapping("getCategories")
    @ResponseBody
    public List<Category> getCategories(int channelId){
        return channelCategoryService.getCategories(channelId);
    }


}
